<template>
	<!-- start:自定义页面内容 -->
	<div>
		<el-card class="box-card">
			<el-image :src="readingUrl"></el-image>
		</el-card>
	</div>
	<!-- end:自定义页面内容 -->
</template>

<script>
	import zzfkUrl from "@images/zzfk/readingIdCard.png";
	import {
		MessageBox
	} from "element-ui";
	import {
		IDCardReader,
		IdInfo
	} from "@api/tools/Hardware.js";

	export default {
		name: "Sfzdk",
		data() {
			return {
				cardNo: "",
				readingUrl: this.loginUrl, //刷卡图标
				idReader: new IDCardReader(),
				idInfo: new IdInfo()
			};
		},
		props: {
			loginUrl: String,
		},
		methods: {
			ReadedCard(data) {
				this.$emit("ReadedIdInfo", data);
			},
			async BeginReadID() {
				controls.log('BeginReadID')
				console.log("%c BeginReadeID", "color:#01DFA5");
				if (this.destroyed) {
					return; // 防止页面关闭后 继续调用
				}
				///////////////////////////// 异步写法
				try {
					/** 读身份证 */
					// await this.idReader.Open();
					let info = await this.idReader.Read();
					// let info = {
					//   ID: 220582198012081311,
					//   Name: "姓名",
					//   Sex: "性别",
					//   Birthday: "生日",
					//   Address: "地址",
					//   Nation: "民族"
					// }
					if (info.ID != "") {
						this.idInfo = info;
						this.ReadedCard(info);
					} else {
						this.timerhandle = setTimeout(() => this.BeginReadID(), 100);
					}
				} catch (e) {
					console.log(`%c error: ${e.message}`, "color:red");
					this.$router.push({
						name: "home"
					});
					MessageBox.alert("未检索到身份证，请重新操作。", {
						confirmButtonText: "确定",
						callback: action => {
							// setTimeout(() => {
							//   this.$router.push({ name: "home" });
							// }, 500);
						}
					});
				}
			},
			async Initynk() {
				await this.ynkReader.Init(0x04, "FFFFFFFFFFFF");
				console.log("set成功");
			}
		},
		mounted() {
			console.log("this.idReader")
			console.log(this.idInfo,'idInfo')
			// const info = {ID: 123}
			// this.ReadedCard(info);
			this.idReader
				.Open()
				.then(() => this.BeginReadID())
				.catch(e => {
					MessageBox.alert(e.message, '读卡错误', {
						confirmButtonText: "确定",
						callback: action => {
							setTimeout(() => {
								this.$router.push({
									name: "home"
								});
							}, 300);
						}
					});
				});
		},
		beforeDestroy() {
			this.idReader.Close();
			if (this.timerhandle != null) {
				clearTimeout(this.timerhandle);
				this.timerhandle = null;
			}
			this.destroyed = true;
		}
	};
</script>

<style scoped>
</style>
